package com.bree.erp.entity;

import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@NamedQuery(name= PartUnit.QUERY_SELECT_ALL_PARTUNIT, query="SELECT p FROM PartUnit p")
@Entity
public class PartUnit implements CrudId<Long>{

	private static final long serialVersionUID = 1L;

	public static final String QUERY_SELECT_ALL_PARTUNIT = "findAllPartUnit";

	public static final int COL_PART_LEN = 50;
	
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private Long id;
	
	@ManyToOne
	private MasterItem masterItem;
	
	@OneToMany(mappedBy="partUnit")
	private Collection<Price> prices;
	
	@OneToOne
	private Unit unit;

	@Column(length=COL_PART_LEN)
	private String part;
	
	/**
	 * Default constructor
	 */
	public PartUnit() {
		
	}
	
	/**
	 * Constructor for template use.
	 * @param id
	 */
	public PartUnit(Long id) {
		this.id = id;
	}
	
	public String getShortName() {
		return masterItem == null ? "" : masterItem.getName();
	}
	
	public String getShortBrand() {
		if(masterItem == null) {
			return "";
		}
		
		if(masterItem.getSeller() == null) {
			return "";
		}
		
		return masterItem.getSeller().getName();
	}
	
	public String getShortUnit() {
		return unit == null ? "" : unit.getCode();
	}
	
	@Override
	public boolean equals(Object object) {
		
		if(this == object) {
			return true;
		}
		
		if(object instanceof PartUnit) {
			PartUnit that = (PartUnit) object;
			return this.id != null && this.id.equals(that.id);
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.id == null ? 0 : this.id.hashCode();
	}
	

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the masterItem
	 */
	public MasterItem getMasterItem() {
		return masterItem;
	}

	/**
	 * @param masterItem the masterItem to set
	 */
	public void setMasterItem(MasterItem masterItem) {
		this.masterItem = masterItem;
	}

	/**
	 * @return the unit
	 */
	public Unit getUnit() {
		return unit;
	}

	/**
	 * @param unit the unit to set
	 */
	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	/**
	 * @return the part
	 */
	public String getPart() {
		return part;
	}

	/**
	 * @param part the part to set
	 */
	public void setPart(String part) {
		this.part = part;
	}
	
	
}
